<template>

    <el-dialog  :visible.sync="dialog" title="模板配置" append-to-body width="550px" @close="close">
      <el-form ref="ruleForm"  :model="form" :rules="rules" size="small" label-width="78px">
        <el-form-item label="科室" >
          <el-select v-model="ware" value-key="id" ref="elDialogSelectRef"  filterable clearable @change="selectWare">
            <el-option
                    v-for="(item,index) in wareList"
                    :value="item"
                    :label="item.name"
                    :key="index"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="author">
          <el-input v-model="form.name" style="width: 420px;" />
        </el-form-item>
        <el-form-item label="默认模板" prop="cover">
          <el-radio-group v-model="form.defaultStatus">

            <el-radio-button  label="1">是</el-radio-button>
            <el-radio-button  label="0">否</el-radio-button>
          </el-radio-group>

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="cancel">清空</el-button>
        <el-button  type="primary" @click="()=> dialog = false">取消</el-button>
        <el-button :loading="loading" type="primary" @click="doSubmit">提交</el-button>
      </div>
    </el-dialog>

</template>

<script>
import { wareList } from  './option'
export default {
  props: {
    option: {
      type: Object,
      defalut: {}
    },
    wareList:{
      type: Array,
      defalut: ()=>[]
    }
  },
  data () {
    return {
      genCodeList: [],
      loading: false,
      dialog: false,
      addDialogVisible: false,
      paramInfo: '',
      ware: {},
      form: Object.assign({ wareId: '-1', name: '', defaultStatus: 0 }, this.option),
      rules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {

  },
  watch:{
    option (newValue,oldvalue){
      if(newValue){
        if(Object.keys(newValue).length>0){
          this.form = Object.assign(this.form,newValue)
          this.ware.name = newValue.wareName
          this.ware.id = newValue.wareId
          // if(this.wareList.length>0){
          //   this.wareList.find((item,index)=>{
          //     if(item.id.toString() === this.form.wareId.toString()){
          //       // this.$refs.elOptionRef[index].dispatch('ElSelect', 'handleOptionClick', [this.$refs.elOptionRef[index], true]);
          //       return true
          //     }
          //     return false
          //   })
          // }
        }else{
          this.form = { wareId: '0', name: '', defaultStatus: 0 }
        }
      }
    }
  },
  methods: {
    init () {
      this.getWareList()
        .then(() => {
          this.form = JSON.parse(this.genCodeList[0].paramInfo)
          this.$emit('update:option', this.form)
        })
    },
    cancel () {
      this.resetForm()
    },
    close () {
      console.log('close')
      if(!!this.form.id){
        this.$emit('update:option', this.form)
      }

    },
    getWareList () {
      // return this.$api.fetchCodeConfList({
      //   current: 1,
      //   size: 1000
      // }).then((res) => {
      //   this.genCodeList = res.records
      // })
    },
    addData () {
      this.$api.addTemplateGroup(this.form)
              .then((res) => {
            this.addDialogVisible = false
            this.$message.success('添加成功！')
            this.$emit('reload')
            this.dialog = false
      }).catch(err => {
      console.log(err.response.data.message)
    }).finally(()=>{
      this.loading = false
    })
    },
    doSubmit () {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          if(this.form.id){
            this.doUpdate()
          }else{
            this.addData()
          }

        } else {
          return false
        }
      })
    },
    doUpdate () {
      this.$api.putTemplateGroup(this.form).then(res => {
        this.$notify({
          title: '更新成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        console.log(err.response.data.message)
      }).finally(()=>{
        this.loading = false
        this.dialog = false
      })

    },
    resetForm () {
      this.dialog = false
      this.$refs['ruleForm'].resetFields()
      this.form = { name: '',  shareStatus: '1' }
    },
    selectWare (data) {
      this.form.wareId = this.ware.id
      this.form.wareName = this.ware.name
    }
  }
}
</script>

<style scoped>

</style>
